package com.soft.link.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * company 重庆庆云石油工程技术有限责任公司
 * FileName RedisService
 * Package com.soft.link.impl
 * Description redis设置
 * author Administrator
 * create 2018-04-18 17:22
 * version V1.0
 */
@Service
public class RedisService {
    @Autowired
    private JedisPool jedisPool;

    /**
     * redis 设置 key
     * @param key 键值
     * @param value 值
     */
    public void set(String key, String value) throws Exception {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
        } finally {
            //返还到连接池
            jedis.close();
        }
    }

    /**
     * @param key 键值
     * @methodname get
     * @Description {根据键值获取值}
     * @author 黄学乾
     * @create 2018/5/3/003 9:16
     */
    public String get(String key) throws Exception  {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);
        } finally {
            //返还到连接池
            jedis.close();
        }
    }

    /**
     * @param key
     * @return
     * @throws Exception
     */
    public boolean exists(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } finally {
            //返还到连接池
            jedis.close();
        }
    }



}
